package cn.iocoder.yudao.module.pay.controller.admin.qrcode;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pay.controller.admin.qrcode.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.qrcode.QrcodeDO;
import cn.iocoder.yudao.module.pay.convert.qrcode.QrcodeConvert;
import cn.iocoder.yudao.module.pay.service.qrcode.QrcodeService;

@Tag(name = "管理后台 - 二维码")
@RestController
@RequestMapping("/pay/qrcode")
@Validated
public class QrcodeController {

    @Resource
    private QrcodeService qrcodeService;

    @PostMapping("/create")
    @Operation(summary = "创建二维码")
    @PreAuthorize("@ss.hasPermission('pay:qrcode:create')")
    public CommonResult<Integer> createQrcode(@Valid @RequestBody QrcodeCreateReqVO createReqVO) {
        return success(qrcodeService.createQrcode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新二维码")
    @PreAuthorize("@ss.hasPermission('pay:qrcode:update')")
    public CommonResult<Boolean> updateQrcode(@Valid @RequestBody QrcodeUpdateReqVO updateReqVO) {
        qrcodeService.updateQrcode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除二维码")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:qrcode:delete')")
    public CommonResult<Boolean> deleteQrcode(@RequestParam("id") Integer id) {
        qrcodeService.deleteQrcode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得二维码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:qrcode:query')")
    public CommonResult<QrcodeRespVO> getQrcode(@RequestParam("id") Integer id) {
        QrcodeDO qrcode = qrcodeService.getQrcode(id);
        return success(QrcodeConvert.INSTANCE.convert(qrcode));
    }

    @GetMapping("/list")
    @Operation(summary = "获得二维码列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('pay:qrcode:query')")
    public CommonResult<List<QrcodeRespVO>> getQrcodeList(@RequestParam("ids") Collection<Integer> ids) {
        List<QrcodeDO> list = qrcodeService.getQrcodeList(ids);
        return success(QrcodeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得二维码分页")
    @PreAuthorize("@ss.hasPermission('pay:qrcode:query')")
    public CommonResult<PageResult<QrcodeRespVO>> getQrcodePage(@Valid QrcodePageReqVO pageVO) {
        PageResult<QrcodeDO> pageResult = qrcodeService.getQrcodePage(pageVO);
        return success(QrcodeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出二维码 Excel")
    @PreAuthorize("@ss.hasPermission('pay:qrcode:export')")
    @OperateLog(type = EXPORT)
    public void exportQrcodeExcel(@Valid QrcodeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<QrcodeDO> list = qrcodeService.getQrcodeList(exportReqVO);
        // 导出 Excel
        List<QrcodeExcelVO> datas = QrcodeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "二维码.xls", "数据", QrcodeExcelVO.class, datas);
    }

}
